using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using SqlWebAdmin.CSharp;

public partial class DeleteColumn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtTableName.Text = HttpUtility.HtmlEncode(Request.QueryString["table"] + "." + Request.QueryString["table"]);
            lblColumnName.Text = HttpUtility.HtmlEncode(Request.QueryString["column"]);
        }


    }

    protected void cmdYes_Click(object sender, EventArgs e)
    {
       
            lblError.Visible = false;

			Server smoServer = SmoHelper.GetServer();
            try
            {
                string databaseName = Request.QueryString["database"];
                Database smoDatabase = smoServer.Databases[databaseName];
                if (smoDatabase != null)
                {
                    string tableName = Request.QueryString["table"];
                    string tableSchema = Request.QueryString["schema"];
                    Microsoft.SqlServer.Management.Smo.Table smoTable = smoDatabase.Tables[tableName, tableSchema];

                    if (smoTable != null)
                    {
                        if (smoTable.Columns.Count == 1)
                        {
                            throw new Exception("Cannot delete last column from table. A table must contain at least one column.");
                        }

                        string columnName = Request.QueryString["column"];
                        Column smoColumn = smoTable.Columns[columnName];
                        if (smoColumn != null)
                        {
                            smoColumn.Drop();

                            Response.Redirect(String.Format("columns.aspx?database={0}&table={1}&schema={2}",
                                HttpUtility.UrlEncode(Request.QueryString["database"]),
                                HttpUtility.UrlEncode(Request.QueryString["table"]),
                                HttpUtility.UrlEncode(Request.QueryString["schema"])), false);
                        }
                        else
                        {
                            Response.Redirect(String.Format("error.aspx?error={0}", Globals.columnNotFoundErrorNumber), false);
                        }
                    }
                    else
                    {
                        Response.Redirect(String.Format("error.aspx?error={0}", Globals.tableNotFoundErrorNumber), false);
                    }
                }
                else
                {
                    Response.Redirect(String.Format("error.aspx?error={0}", Globals.databaseNotFoundErrorNumber), false);
                }
            }
            catch (SmoException smoEx)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                lblError.Visible = true;
            }
            catch (System.Exception ex)
            {
                lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                lblError.Visible = true;
			}
			finally
			{
				smoServer.ConnectionContext.Disconnect();
			}

        
    }

    protected void cmdNo_Click(object sender, EventArgs e)
    {
        Response.Redirect(String.Format("columns.aspx?database={0}&table={1}&schema={2}",
            Server.UrlEncode(Request.QueryString["database"]),
            Server.UrlEncode(Request.QueryString["table"]),
            Server.UrlEncode(Request.QueryString["schema"])), false);
    }
}
